package com.netty.clientserver.strategy.factory;

import com.netty.clientserver.packet.base.Packet;
import com.netty.clientserver.strategy.TypeStrategy;
import com.netty.clientserver.strategy.impl.RegisterTypeStrategyImpl;
import com.netty.common.entity.User;
import com.netty.common.enums.TypeEnum;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import sun.management.snmp.jvminstr.JvmThreadInstanceEntryImpl;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/** 策略工厂
 * @author LiuXiaoYu
 * @date 2022/2/3- 14:27
 */
@Service
public class TypeStrategyFactory {
    private final Map<String,TypeStrategy> strategyMap=new ConcurrentHashMap<>();

    /**构造时就放入
     * Map中key->spring容器名 value->策略实现类
     * @param strategyMap
     */
    @Autowired
    public TypeStrategyFactory(Map<String,TypeStrategy> strategyMap) {
        strategyMap.forEach(this.strategyMap::put);
    }


    /**
     * 通过给出spring容器名来执行对象的方法
     * @param type
     * @return
     */
    public Packet doExecute(Channel channel,String type, String content){
       return strategyMap.get(type).execute(channel,type,content);
    }

}
